using System;
using System.Drawing;
using System.Collections;
using System.Reflection;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Windows.Forms.Design;
using System.Windows.Forms;
using EastReportDesigner;

namespace FormDesigner
{
	public class PopupMenuManager
	{
		IMenuCommandService menuCommandService;
		ISelectionService SelectionService;

		ImageList imageList = new ImageList();

		EastReportDesigner.Library.Menus.MenuCommand mcSpace = new EastReportDesigner.Library.Menus.MenuCommand("-");

		EastReportDesigner.Library.Menus.MenuCommand mcCut;
		EastReportDesigner.Library.Menus.MenuCommand mcCopy;
		EastReportDesigner.Library.Menus.MenuCommand mcPaste;
		EastReportDesigner.Library.Menus.MenuCommand mcDelete;

		EastReportDesigner.Library.Menus.MenuCommand mcBringToFront;
		EastReportDesigner.Library.Menus.MenuCommand mcSendToBack;

		EastReportDesigner.Library.Menus.MenuCommand mcAlign = new EastReportDesigner.Library.Menus.MenuCommand(Language.GetString("Menu.Align"));
		EastReportDesigner.Library.Menus.MenuCommand mcAlignLeft;
		EastReportDesigner.Library.Menus.MenuCommand mcAlignRight;
		EastReportDesigner.Library.Menus.MenuCommand mcAlignHorizontalCenters;
		EastReportDesigner.Library.Menus.MenuCommand mcAlignTop;
		EastReportDesigner.Library.Menus.MenuCommand mcAlignBottom;
		EastReportDesigner.Library.Menus.MenuCommand mcAlignVerticalCenters;

		public PopupMenuManager()
		{
			System.Reflection.Assembly myAssembly = System.Reflection.Assembly.GetAssembly(typeof(PopupMenuManager));
			System.IO.Stream imageStream;
			Bitmap image;

			imageList.ColorDepth = ColorDepth.Depth24Bit;
			imageList.TransparentColor = Color.White;
			imageList.ImageSize = new System.Drawing.Size(16, 16);

			imageStream = myAssembly.GetManifestResourceStream("EastReportDesigner.FormDesigner.Gui.Image.Cut.bmp");
			image = new Bitmap(imageStream);
			imageList.Images.Add(image);
			imageStream = myAssembly.GetManifestResourceStream("EastReportDesigner.FormDesigner.Gui.Image.Copy.bmp");
			image = new Bitmap(imageStream);
			imageList.Images.Add(image);
			imageStream = myAssembly.GetManifestResourceStream("EastReportDesigner.FormDesigner.Gui.Image.Paste.bmp");
			image = new Bitmap(imageStream);
			imageList.Images.Add(image);
			imageStream = myAssembly.GetManifestResourceStream("EastReportDesigner.FormDesigner.Gui.Image.Delete.bmp");
			image = new Bitmap(imageStream);
			imageList.Images.Add(image);
			imageStream = myAssembly.GetManifestResourceStream("EastReportDesigner.FormDesigner.Gui.Image.BringToFront.bmp");
			image = new Bitmap(imageStream);
			imageList.Images.Add(image);
			imageStream = myAssembly.GetManifestResourceStream("EastReportDesigner.FormDesigner.Gui.Image.SendToBack.bmp");
			image = new Bitmap(imageStream);
			imageList.Images.Add(image);
			imageStream = myAssembly.GetManifestResourceStream("EastReportDesigner.FormDesigner.Gui.Image.AlignLeft.bmp");
			image = new Bitmap(imageStream);
			imageList.Images.Add(image);
			imageStream = myAssembly.GetManifestResourceStream("EastReportDesigner.FormDesigner.Gui.Image.AlignVertically.bmp");
			image = new Bitmap(imageStream);
			imageList.Images.Add(image);
			imageStream = myAssembly.GetManifestResourceStream("EastReportDesigner.FormDesigner.Gui.Image.AlignRight.bmp");
			image = new Bitmap(imageStream);
			imageList.Images.Add(image);
			imageStream = myAssembly.GetManifestResourceStream("EastReportDesigner.FormDesigner.Gui.Image.AlignTop.bmp");
			image = new Bitmap(imageStream);
			imageList.Images.Add(image);
			imageStream = myAssembly.GetManifestResourceStream("EastReportDesigner.FormDesigner.Gui.Image.AlignHorizontally.bmp");
			image = new Bitmap(imageStream);
			imageList.Images.Add(image);
			imageStream = myAssembly.GetManifestResourceStream("EastReportDesigner.FormDesigner.Gui.Image.AlignBottom.bmp");
			image = new Bitmap(imageStream);
			imageList.Images.Add(image);

			mcCut = new EastReportDesigner.Library.Menus.MenuCommand(Language.GetString("Menu.Cut"), imageList, 0, new EventHandler(this.DoCommand));
			mcCut.Tag = MenuCommands.Cut;
			mcCopy = new EastReportDesigner.Library.Menus.MenuCommand(Language.GetString("Menu.Copy"), imageList, 1, new EventHandler(this.DoCommand));
			mcCopy.Tag = MenuCommands.Copy;
			mcPaste = new EastReportDesigner.Library.Menus.MenuCommand(Language.GetString("Menu.Paste"), imageList, 2, new EventHandler(this.DoCommand));
			mcPaste.Tag = MenuCommands.Paste;
			mcDelete = new EastReportDesigner.Library.Menus.MenuCommand(Language.GetString("Menu.Delete"), imageList, 3, new EventHandler(this.DoCommand));
			mcDelete.Tag = MenuCommands.Delete;

			mcBringToFront = new EastReportDesigner.Library.Menus.MenuCommand(Language.GetString("Menu.BringToFront"), imageList, 4, new EventHandler(this.DoCommand));
			mcBringToFront.Tag = MenuCommands.BringToFront;
			mcSendToBack = new EastReportDesigner.Library.Menus.MenuCommand(Language.GetString("Menu.SendToBack"), imageList, 5, new EventHandler(this.DoCommand));
			mcSendToBack.Tag = MenuCommands.SendToBack;

			mcAlignLeft = new EastReportDesigner.Library.Menus.MenuCommand(Language.GetString("Menu.AlignLeft"), imageList, 6, new EventHandler(this.DoCommand));
			mcAlignLeft.Tag = MenuCommands.AlignLeft;
			mcAlignVerticalCenters = new EastReportDesigner.Library.Menus.MenuCommand(Language.GetString("Menu.AlignCenter"), imageList, 7, new EventHandler(this.DoCommand));
			mcAlignVerticalCenters.Tag = MenuCommands.AlignVerticalCenters;
			mcAlignRight = new EastReportDesigner.Library.Menus.MenuCommand(Language.GetString("Menu.AlignRight"), imageList, 8, new EventHandler(this.DoCommand));
			mcAlignRight.Tag = MenuCommands.AlignRight;
			mcAlignTop = new EastReportDesigner.Library.Menus.MenuCommand(Language.GetString("Menu.AlignTop"), imageList, 9, new EventHandler(this.DoCommand));
			mcAlignTop.Tag = MenuCommands.AlignTop;
			mcAlignHorizontalCenters = new EastReportDesigner.Library.Menus.MenuCommand(Language.GetString("Menu.AlignMiddle"), imageList, 10, new EventHandler(this.DoCommand));
			mcAlignHorizontalCenters.Tag = MenuCommands.AlignHorizontalCenters;
			mcAlignBottom = new EastReportDesigner.Library.Menus.MenuCommand(Language.GetString("Menu.AlignBottom"), imageList, 11, new EventHandler(this.DoCommand));
			mcAlignBottom.Tag = MenuCommands.AlignBottom;

			mcAlign.MenuCommands.Add(mcAlignLeft);
			mcAlign.MenuCommands.Add(mcAlignVerticalCenters);
			mcAlign.MenuCommands.Add(mcAlignRight);
			mcAlign.MenuCommands.Add(mcSpace);
			mcAlign.MenuCommands.Add(mcAlignTop);
			mcAlign.MenuCommands.Add(mcAlignHorizontalCenters);
			mcAlign.MenuCommands.Add(mcAlignBottom);


		}

		public void ShowPopupMenu(IMenuCommandService MenuCommandService, CommandID menuID, int x, int y)
		{
			menuCommandService = MenuCommandService;
			MenuCommandService mcs = (MenuCommandService)menuCommandService;
			SelectionService = (ISelectionService)mcs.host.GetService(typeof(ISelectionService));
			
			EastReportDesigner.Library.Menus.PopupMenu popup = new EastReportDesigner.Library.Menus.PopupMenu();
			popup.ToolbarControlStyle = EastReportDesigner.Library.Toolbar.ToolbarControlStyle.Office2003;

			if(menuID == MenuCommands.ContainerMenu)
			{
				popup.MenuCommands.Add(mcPaste);
			}

			if(menuID == MenuCommands.SelectionMenu)
			{
				popup.MenuCommands.Add(mcBringToFront);
				popup.MenuCommands.Add(mcSendToBack);
				popup.MenuCommands.Add(mcSpace);

				if(SelectionService.SelectionCount > 1)
				{
					popup.MenuCommands.Add(mcAlign);
					popup.MenuCommands.Add(mcSpace);
				}

				popup.MenuCommands.Add(mcCut);
				popup.MenuCommands.Add(mcCopy);
				popup.MenuCommands.Add(mcPaste);
				popup.MenuCommands.Add(mcDelete);

				if(SelectionService.SelectionCount == 1)
				{
					if(MenuCommandService.Verbs.Count > 0)
					{
						popup.MenuCommands.Add(mcSpace);
						for(int i=0; i < MenuCommandService.Verbs.Count; i++)
						{
							EastReportDesigner.Library.Menus.MenuCommand mc = new EastReportDesigner.Library.Menus.MenuCommand(MenuCommandService.Verbs[i].Text, new EventHandler(DoVerb));
							mc.Tag = MenuCommandService.Verbs[i];
							popup.MenuCommands.Add(mc);
						}
					}
				}
			}

			popup.TrackPopup(new System.Drawing.Point(x, y));
		}
		
		private void DoCommand(object sender, EventArgs e)
		{
			if(sender is EastReportDesigner.Library.Menus.MenuCommand)
			{
				EastReportDesigner.Library.Menus.MenuCommand mc = (EastReportDesigner.Library.Menus.MenuCommand)sender;
				CommandID cID = (CommandID)mc.Tag;
				this.menuCommandService.GlobalInvoke(cID);
			}
		}
		private void DoVerb(object sender, EventArgs e)
		{
			if(sender is EastReportDesigner.Library.Menus.MenuCommand)
			{
				EastReportDesigner.Library.Menus.MenuCommand mc = (EastReportDesigner.Library.Menus.MenuCommand)sender;
				DesignerVerb dv = (DesignerVerb)mc.Tag;
				
				this.menuCommandService.GlobalInvoke(dv.CommandID);
			}
		}

		void AddItems(ArrayList items, IList itemsToAdd) 
		{
			foreach (object item in itemsToAdd) 
			{
				if (item is MenuCommand[]) 
				{
					AddItems(items, (MenuCommand[])item);
				} 
				else 
				{
					items.Add(item);
				}
			}
		}
	}
}
